package ytdp.careagle.com.ytdp.myview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

import java.util.List;

import ytdp.careagle.com.ytdp.R;

/**
 * Created by admin on 2017/9/25.
 * LinearLayout
 */

public class MyTableView extends View {
    private int textWith;
    private List<String> list;
    private TextPaint paint;
    private int textHeight;
    private int lineNUmber = 0;
    private int boundTextheight;
    private int textColor;
    private int lineColor;
    private int rectBg;
    private boolean isDrawedView;//是否已经画好了
    private Handler handler;//是否已经画好了
    private int firstColor;
    private boolean isTitle = false;
    private Paint linePaint;

    public void setFirstOnClickListener(FirstOnClickListener firstOnClickListener) {
        this.firstOnClickListener = firstOnClickListener;
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 0:
//                        MyTableView.this.firstOnClickListener.onFirstClick();
                        break;
                }
            }
        };
    }

    private FirstOnClickListener firstOnClickListener;

    public MyTableView(Context context) {
        this(context, null);
    }

    public MyTableView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        textWith = (int) getResources().getDimension(R.dimen.dp90);
        textHeight = (int) getResources().getDimension(R.dimen.dp30);
        paint = new TextPaint();
        linePaint = new Paint();
        linePaint.setStrokeWidth(2);
        linePaint.setStyle(Paint.Style.STROKE);

        paint.setTextSize(getResources().getDimension(R.dimen.sp12));
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setAntiAlias(true);
        textColor = getResources().getColor(R.color.text_title);
        firstColor = getResources().getColor(R.color.text_title);
//        firstColor = getResources().getColor(R.color.guestPaint_color_wrong);
        lineColor = getResources().getColor(R.color.text_gray);
        rectBg = getResources().getColor(R.color.rect_bg);
        paint.setColor(textColor);
        Rect rect = new Rect();
        String text = "哈";
        paint.getTextBounds(text, 0, text.length(), rect);
        boundTextheight = rect.height();//单个字体的高度
    }

    public void initData(List<String> list, int i) {
        this.list = list;
        lineNUmber = i;
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(textWith * list.size(), textHeight);
        this.setLayoutParams(layoutParams);
        postInvalidate();
        isDrawedView = true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                String text = list.get(i);
                if (TextUtils.isEmpty(text)) {
                    text = "?";
                }
                if (i == 0 || lineNUmber == 0) {
                    paint.setColor(rectBg);
                    canvas.drawRect(textWith * i, 0, textWith * (i + 1), textHeight, paint);
                    isTitle = true;
                }
                paint.setColor(lineColor);
                canvas.drawRect(textWith * (i), 0, textWith * (i + 1), textHeight, linePaint);
//                canvas.drawLine(0, 0, textWith * (i + 1), 0, paint);//上
//                canvas.drawLine(textWith * (i + 1), 0, textWith * (i + 1), textHeight, paint);//右
//                canvas.drawLine(0, 0, 0, textHeight, paint);//左
//                canvas.drawLine(0, textHeight, textWith * (i + 1), textHeight, paint);//下
//                if (isTitle){
//                    linePaint.setStrokeWidth(5);
//                    canvas.drawLine(0, 0, textWith * (i + 1), 0, paint);//上
//                    canvas.drawLine(textWith * (i + 1), 0, textWith * (i + 1), textHeight, paint);//右
//                    canvas.drawLine(0, 0, 0, textHeight, paint);//左
//                    canvas.drawLine(0, textHeight, textWith * (i + 1), textHeight, paint);//下
//                }
                if (i == 0 && firstOnClickListener != null) {
                    paint.setColor(firstColor);
                } else {
                    paint.setColor(textColor);
                }
                StaticLayout myStaticLayout = new StaticLayout(text, paint, textWith, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
                canvas.save();
                canvas.translate(textWith * i + textWith / 2, textHeight / 2 - boundTextheight / 2 * myStaticLayout.getLineCount());
                myStaticLayout.draw(canvas);
                canvas.restore();
                isTitle = false;
            }
        }
    }

    public boolean isDrawedView() {
        return isDrawedView;
    }

    @Override
    public boolean onTouchEvent(MotionEvent motionEvent) {
        if (firstOnClickListener != null && motionEvent.getX() <= textWith) {
            switch (motionEvent.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    handler.removeMessages(1);
                    handler.sendEmptyMessageDelayed(0, 500);
                    break;
            }
            return true;
        }
        return super.onTouchEvent(motionEvent);
    }

    public interface FirstOnClickListener {
        void onFirstClick();
    }
}
